package com.souku.finance.application.ui.invite;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.manager.RequestManager;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.souku.finance.application.R;
import com.souku.finance.application.adapter.RedpacketSelectAdapter;
import com.souku.finance.application.base.BaseActivity;
import com.souku.finance.application.bean.RedpacketBean;
import com.souku.finance.application.utils.Constant;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.MyTextUtil;
import com.souku.finance.application.utils.ParamUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.http.volley.ReqListener;
import com.souku.finance.application.http.volley.SuccessError;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Android on 2017/1/13.
 */

public class RedpacketActivity extends BaseActivity implements View.OnClickListener {

    protected RelativeLayout topBack;
    protected TextView topTitle;
    protected PullToRefreshListView pullList;
    private RedpacketSelectAdapter redpacketAdapter;
    private List<RedpacketBean.ListBean> redlist = new ArrayList<>();
    private RedpacketBean.ListBean selectBean;
    private int page = 1;
    private double inviteMoney;
    private int days;

    @Override
    public void internet() {
        pullList.setRefreshing(true);
    }

    @Override
    protected void onResume() {
        super.onResume();
        pullList.setRefreshing(true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.act_redpacket);
        days = getIntent().getIntExtra(Constant.DefaultMost, 0);
        inviteMoney = getIntent().getDoubleExtra(Constant.DefaultMore, 0);
        selectBean = getIntent().getParcelableExtra(Constant.Default);
        initView();
        topTitle.setText("选择红包");
        redpacketAdapter = new RedpacketSelectAdapter(redlist, selectBean);
        pullList.setMode(PullToRefreshBase.Mode.BOTH);
        pullList.setAdapter(redpacketAdapter);
        pullList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                redlist.clear();
                page = 1;
                getEcv_list();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                getEcv_list();
            }
        });
        pullList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                RedpacketBean.ListBean bean = (RedpacketBean.ListBean) parent.getItemAtPosition(position);
                int minThreshold = Integer.parseInt(bean.getMinThreshold());
                if (minThreshold != 0) {
                    if (inviteMoney * 100 < minThreshold) {
                        ToastUtil.show("投资满" + MyTextUtil.setMoneyToElement(bean.getMinThreshold()) + "元可用");
                        return;
                    }
                }
                int minThresholdDealDuration = Integer.parseInt(bean.getThresholdDealDuration());
                if (minThresholdDealDuration != 0) {
                    if (days < minThresholdDealDuration) {
                        ToastUtil.show("投资超" + bean.getThresholdDealDuration() + "天可用");
                        return;
                    }
                }
                if (selectBean == null || !bean.getId().equals(selectBean.getId())) {
                    selectBean = bean;
                } else {
                    selectBean = null;
                }
                redpacketAdapter.setChanged(selectBean);
                check(false);
            }
        });
    }

    private void check(boolean isFinish) {
        if (selectBean != null || isFinish) {
            Intent intent = new Intent();
            intent.putExtra(Constant.Default, selectBean);
            setResult(0, intent);
            finish();
        }
    }


    private void getEcv_list() {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("pg_no", page + "");
        map.put("type", "1");
        RequestManager.getInstance().post(UrlConstact.ECV_LIST, ParamUtil.getSing(map), new ReqListener(this, new SuccessError() {
            @Override
            public void onSuccess(String response) {
                pullList.onRefreshComplete();
                if (JsonUtil.getCode(response) == 1) {
                    RedpacketBean bean = JsonUtil.fastBean(JsonUtil.getData(response).toString(), RedpacketBean.class);
                    if (bean.getList().size() == 0) {
                        ToastUtil.show("没有更多内容");
                    } else {
                        redlist.addAll(bean.getList());
                        redpacketAdapter.notifyDataSetChanged();
                    }
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }

            @Override
            public void onError(String errorMsg) {
                ToastUtil.show("请求失败");
                pullList.onRefreshComplete();
            }
        }), ParamUtil.getHeaders());
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.top_back) {
            check(true);
        }
    }

    private void initView() {
        topBack = (RelativeLayout) findViewById(R.id.top_back);
        topBack.setOnClickListener(RedpacketActivity.this);
        topTitle = (TextView) findViewById(R.id.top_title);
        pullList = (PullToRefreshListView) findViewById(R.id.pull_list);
    }

    @Override
    public void onBackPressed() {
        check(true);
    }
}
